package com.mazaiting.generator.controller;

import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.generator.domain.query.PreviewQuery;
import com.mazaiting.generator.domain.query.TableQuery;
import com.mazaiting.generator.domain.vo.PreviewVO;
import com.mazaiting.generator.domain.vo.TableDefaultVO;
import com.mazaiting.generator.domain.vo.TableVO;
import com.mazaiting.generator.service.ITableService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 数据表管理
 */
@RestController
@RequestMapping("generator/v1/table")
@AllArgsConstructor
public class TableController {
    private final ITableService tableService;

    /**
     * 分页
     *
     * @param tableQuery 查询参数
     */
    @GetMapping("page")
    public BasePageVO<TableVO> page(TableQuery tableQuery) throws Exception {
        return tableService.page(tableQuery);
    }

    /**
     * 根据表名与文件类型预览
     * @param previewQuery 预览查询参数
     * @return 文件内容
     */
    @PostMapping("preview")
    public PreviewVO getContentByFileType(@Valid PreviewQuery previewQuery) throws BusinessException, EmptyObjectException {
        return tableService.getContentByFileType(previewQuery);
    }

    /**
     * 获取默认的数据
     * @return 表默认数据
     */
    @GetMapping("default")
    public TableDefaultVO getDefaultData() {
        return tableService.getDefaultData();
    }

}
